package it.unina.android.trip_track.database.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import java.util.ArrayList;
import java.util.List;

import it.unina.android.trip_track.database.SQLiteHelper;
import it.unina.android.trip_track.entity.Percorso;

/**
 * TODOs DAO object.
 *
 * @author itcuties
 */
public class PercorsoDAO implements IDAO<Percorso> {

    // Percorso table name
    private static final String TABLE_NAME = "percorso";
    private static final String KEY_ID = "id";
    private static final String COL_NOME = "nome";
    private static final String COL_NOTE = "note";
    private String[] columns = new String[]{KEY_ID, COL_NOME,COL_NOTE};

    //IMPORTANTE: Da usare in SQLiteHelper nei metodi onCreate() ed onUpgrade()
    public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NOME + " TEXT NOT NULL,"+COL_NOTE+" TEXT NULL);";
    public static final String UPGRADE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

    private SQLiteDatabase db;
    private SQLiteHelper dbHelper;

    public PercorsoDAO(Context context) {
        dbHelper = new SQLiteHelper(context);
        db = dbHelper.getWritableDatabase();
    }

    // Close the db
    public void close() {
        db.close();
    }

    /**
     * Create new Percorso object
     *
     * @param percorso
     */
    public void insert(Percorso percorso) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_NOME, percorso.getNome());
        contentValues.put(COL_NOTE, percorso.getNote());
        // Insert into DB
        db.insert(TABLE_NAME, null, contentValues);
    }

    // Updating single percorso
    public int update(Percorso percorso) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_NOME, percorso.getNome());
        contentValues.put(COL_NOTE, percorso.getNote());
        // updating row
        return db.update(TABLE_NAME, contentValues, KEY_ID + " = ?", new String[]{String.valueOf(percorso.getId())});
    }

    /**
     * Delete TODO object
     *
     * @param id percorso
     */
    public void delete(int id) {
        // Delete from DB where id match
        db.delete(TABLE_NAME, KEY_ID + " = " + id, null);
    }

    // Getting single todo
    public Percorso findById(int id) {
        Cursor cursor = db.query(TABLE_NAME, columns, KEY_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Percorso percorso = new Percorso();
        percorso.setId(Integer.parseInt(cursor.getString(0)));
        percorso.setNome(cursor.getString(1));
        percorso.setNote(cursor.getString(2));

        // return todo
        cursor.close();
        db.close();

        return percorso;
    }

    /**
     * Get all TODOs.
     *
     * @return
     */
    public List<Percorso> findAll() {
        List<Percorso> list = new ArrayList<Percorso>();

        // Name of the columns we want to select

        // Query the database
        Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
        cursor.moveToFirst();

        // Iterate the results
        while (!cursor.isAfterLast()) {
            Percorso percorso = new Percorso();
            // Take values from the DB
            percorso.setId(cursor.getInt(0));
            percorso.setNome(cursor.getString(1));
            percorso.setNote(cursor.getString(2));

            // Add to the DB
            list.add(percorso);

            // Move to the next result
            cursor.moveToNext();
        }

        return list;
    }

    public int count() {
        //String countQuery = "SELECT  * FROM " + TABLE_NAME;
        String countQuery = "SELECT  count(*) FROM " + TABLE_NAME;
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
        return cursor.getInt(0);
        // return count
        //return cursor.getCount();
    }

}
